package cn.com.onlinetool.jt809.gsafety;

import cn.com.onlinetool.jt809.bean.Message;
import cn.com.onlinetool.jt809.util.ByteArrayUtil;
import cn.com.onlinetool.jt809.util.PacketUtil;

/**
 * @author xiaodonghong
 * @description 根据jt809-2019生成数据
 * @date 2023/6/19-15:50
 */
public class PaswDemo {
    public static void main(String[] args) {

        PaswDemo.generateLoginhex();

    }

    public static String generateLoginhex() {
        Message message = new Message();
        Message.MessageHead msgHead = new Message.MessageHead();
        msgHead.setMsgId((short) 4097);
        msgHead.setMsgSn(1);
        msgHead.setEncryptFlag(0);
        msgHead.setEncryptKey(0);
        msgHead.setMsgLength(72);
        msgHead.setMsgGnssCenterId(40010);
        byte[] versionFlag = {1,2,15};
        msgHead.setVersionFlag(versionFlag);
        byte [] crc = {20,-33};
        message.setCrcCode(crc);
        message.setHeadFlag(91);
        message.setEndFlag(93);
        byte[] msgBody = {0, 0, 12, 24, 49, 55, 57, 52, 53, 54, 55, 56, 49, 50, 49, 46, 52, 51, 46, 49, 56, 54, 46, 49, 53, 53, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 15};

        message.setMsgBody(msgBody);
        message.setMsgHead(msgHead);

        System.out.println(PacketUtil.message2Bytes(message));
        System.out.println(ByteArrayUtil.bytes2HexStr(PacketUtil.message2Bytes(message)));

        byte[] body = message.getMsgBody();
        int userId = ByteArrayUtil.bytes2int(ByteArrayUtil.subBytes(body, 0, 4));

        String password = ByteArrayUtil.bytes2gbkString(ByteArrayUtil.subBytes(body, 4, 8));
        password = password.replaceAll("\\u0000", "");  //清除掉空格
        return  ByteArrayUtil.bytes2HexStr(PacketUtil.message2Bytes(message));
    }
}
